package top.i51213.fastlink;

import androidx.activity.result.ActivityResultLauncher;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Spinner;

public class MainActivity extends AppCompatActivity{
    Spinner spinner;
    TwoTextView tv_runner,tv_data;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spinner=findViewById(R.id.main_spinner);
        initView();
    }
    public String getDataType(){
        return "image/*";
    }
    private void initView(){
        tv_runner=findViewById(R.id.main_action_runner);
        tv_data=findViewById(R.id.main_action_data);

        Contract<Integer, Uri> contract=new Contract.Builder<Integer, Uri>()
                .setCreateIntent((context, input) -> {
                    Intent intent=new Intent(Intent.ACTION_GET_CONTENT);
                    intent.setType(getDataType());
                    return intent;
                })
                .setParseResult((resultCode,intent)->{
                    if(resultCode==RESULT_OK){
                        return intent.getData();
                    }
                    return null;
                })
                .build();
        ActivityResultLauncher<Integer> launcher= this.registerForActivityResult(contract, result->{
            System.out.println(result);
            String displayName=Util.getUriDisplayName(this,result);
            System.out.println(displayName);
            tv_data.setText(displayName);
            tv_data.setData(getDataType()+"|"+ result.toString());
        });
        findViewById(R.id.main_btn_find_data).setOnClickListener(((v)->{
            launcher.launch(0);
        }));
    }
}